<?php

class Application_Model_Question {

    protected $_question;
    protected $_questionnaire;
    protected $_id;

    public function __construct(array $options = null) {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    public function __set($name, $value) {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid questionnaire property');
        }
        $this->$method($value);
    }

    public function __get($name) {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid questionnaire property');
        }
        return $this->$method();
    }

    public function setOptions(array $options) {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

    public function setQuestion($question) {
        $this->_question = str_replace('\\', '', (string) $question);
        return $this;
    }

    public function getQuestion() {
        return $this->_question;
    }

    public function setQuestionnaire($questionnaire) {
        $this->_questionnaire = (int) $questionnaire;
        return $this;
    }

    public function getQuestionnaire() {
        return $this->_questionnaire;
    }

    public function setId($id) {
        $this->_id = (int) $id;
        return $this;
    }

    public function getId() {
        return $this->_id;
    }

    public function getResult() {
        /*
         * Returns an array with the correct responses
         */
//      $logger = new Zend_Log();
//      $writer = new Zend_Log_Writer_Stream('php://output');
//      $logger->addWriter($writer);

        $question = $this->getQuestion();      
        $result = array();
        if ($question != null && $question != '') {
            $response_init = strpos($question, "{");
            $response_end = strpos($question, "}");
            
            if ($response_init >= 0 && $response_end > 0) {
                $pre_question = substr($question, 0, $response_init);
                $post_question = substr($question, $response_end + 1, strlen($question) - 1);

                $responses_str = substr($question, $response_init + 1, $response_end - strlen($question));
                
                $responses = explode("|", $responses_str);
                
                foreach($responses as $re) {
                    
                    if (strpos($re, '*') !== false) {
                        
                        $aux = str_replace('*', '', $re);
                        $aux = str_replace('\\', '', $aux);
                        $result[] = $aux;

                    }
                }
            }
        }

        return $result;
    }

}

